package vn.vttu.baocaocuoiki.NguyenHoangTrung;

import vn.vttu.baocaocuoiki.R;
import vn.vttu.baocaocuoiki.R.layout;
import vn.vttu.baocaocuoiki.R.menu;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ThongTinTVActivity extends Activity implements OnClickListener{
	
	private EditText edtTen, edtTuoi, edtSDT;
	private Button btnLuu, btnXoa, btnCapNhat, btnThoat;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_thong_tin_tv);
		ThamChieu();
		
		SharedPreferences shared = getSharedPreferences("ThongTinThanhVien", MODE_PRIVATE);
		String ten = shared.getString("Ten",null);
		String tuoi = shared.getString("Tuoi",null);
		String sdt = shared.getString("SDT",null);
		if(ten != null && tuoi != null && sdt != null)
		{
			edtTen.setText(ten);
			edtTuoi.setText(tuoi);
			edtSDT.setText(sdt);
		}
	}
	private void ThamChieu()
	{
		edtTen = (EditText)findViewById(R.id.edtTen);
		edtTuoi = (EditText)findViewById(R.id.edtTuoi);
		edtSDT = (EditText)findViewById(R.id.edtSDT);
		btnLuu = (Button)findViewById(R.id.btnLuu);
		btnXoa = (Button)findViewById(R.id.btnXoa);
		btnThoat = (Button)findViewById(R.id.btnThoat);
		btnCapNhat = (Button)findViewById(R.id.btnCapNhat);
		btnLuu.setOnClickListener(this);
		btnXoa.setOnClickListener(this);
		btnCapNhat.setOnClickListener(this);
		btnThoat.setOnClickListener(this);
	}
	private void Luu(View v)
	{
		String ten = edtTen.getText().toString();
		String tuoi = edtTuoi.getText().toString();
		String sdt = edtSDT.getText().toString();
		SharedPreferences sp = (SharedPreferences)getSharedPreferences("ThongTinThanhVien", MODE_PRIVATE);
		Editor edit = sp.edit();
		
		edit.putString("Ten",ten);
		edit.putString("Tuoi",tuoi);
		edit.putString("SDT",sdt);
		edit.commit();
		Toast.makeText(getApplicationContext(), "Lưu thành công", 1000).show();
	}
	private void Xoa(View v)
	{
		String ten = edtTen.getText().toString();
		String tuoi = edtTuoi.getText().toString();
		String sdt = edtSDT.getText().toString();
		SharedPreferences sp = (SharedPreferences)getSharedPreferences("ThongTinThanhVien", MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.clear();
		edit.commit();
		edtTen.setText("");
		edtTuoi.setText("");
		edtSDT.setText("");
		Toast.makeText(getApplicationContext(), "Xóa thành công", 1000).show();
	}
	private void CapNhat(View v)
	{
		String ten = edtTen.getText().toString();
		String tuoi = edtTuoi.getText().toString();
		String sdt = edtSDT.getText().toString();
		SharedPreferences sp = (SharedPreferences)getSharedPreferences("ThongTinThanhVien", MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putString("Ten", ten);
		edit.putString("Tuoi", tuoi);
		edit.putString("SDT", sdt);
		edit.commit();
		Toast.makeText(getApplicationContext(), "Cập nhật thành công", 1000).show();
	}
	
	private void Thoat(View v) {
		// TODO Auto-generated method stub
		finish();
		System.exit(0);
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(btnLuu.isPressed())
		{
		Luu(btnLuu);
		}
		else if (btnXoa.isPressed())
		{
			Xoa(btnXoa);
		}
		else if(btnCapNhat.isPressed())
		{
			CapNhat(btnCapNhat);
		}
		else if(btnThoat.isPressed())
		{
			Thoat(btnThoat);
		}
	}
}

